<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\api\energy;

use think\App;
use app\controller\api\Base;
use app\validate\energy\EnergyPlanValidate;
use app\common\repositories\energy\EnergyPlanRepository;


class EnergyPlan extends Base
{
    protected $repository;

    public function __construct(App $app, EnergyPlanRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    public function list()
    {
        $where = $this->request->param([
            'keywords' => '',
            'property_id' => '',
        ]);
        [$page, $limit] = $this->getPage();
        $where['add_uid'] = $this->request->userId();
        $res = $this->repository->getApiList($where, $page, $limit, $this->request->propertyId);
        return $this->success($res);
    }

    /**
     * 获取计费方案 呢称
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-07 17:32
     */
    public function listData()
    {
        $where = $this->request->param([
            'energy_type' => '',
        ]);
        $where['add_uid'] = $this->request->userId();
        $res = $this->repository->getApiPlanData($where, $this->request->propertyId);
        return $this->success($res);
    }


    public function add()
    {
        $param = $this->paramValues();
        validate(EnergyPlanValidate::class)->check($param);
        if ($this->repository->propertyFieldExists($param['property_id'], 'plan_name', $param['plan_name'])) {
            return $this->error(' 已存在 ' . $param['plan_name'] . ' 方案');
        }
        $this->repository->addInfo($param, $this->request->propertyId, $this->request->userId());
        return app('api_return')->success('添加成功');
    }

    protected function paramValues()
    {
        $param = $this->request->param([
            'property_id' => '',
            'plan_name' => '',
            'mode_type' => '',
            'basic_price' => '',
            'basic_price_j' => '',
            'basic_price_f' => '',
            'basic_price_p' => '',
            'basic_price_g' => '',
            'service_switch' => '',
            'service_name' => '',
            'service_price' => '',
            'service_price_j' => '',
            'service_price_f' => '',
            'service_price_p' => '',
            'service_price_g' => '',
            'loss_switch' => '',
            'loss_name' => '',
            'loss_per' => '',
            'is_show' => '',
            'remark' => ''
        ]);
        switch ($param['mode_type']) {
            case 1:
                $param['basic_price'] = $param['basic_price'];
                $param['stair_price'] = [];
                break;
            case 2:
                $param['basic_price'] = '';
                $param['stair_price'] = [];
                $param['basic_price_j'] = $param['basic_price_j'];
                $param['basic_price_f'] = $param['basic_price_f'];
                $param['basic_price_p'] = $param['basic_price_p'];
                $param['basic_price_g'] = $param['basic_price_g'];
                break;
            case 3:
                $param['basic_price'] = '';
                $param['stair_price'] = $param['stair_price'];
                break;
            default:
                return $this->error('方案类型错误');
                break;
        }
        return $param;
    }

    public function edit(int $id)
    {
        $param = $this->paramValues();
        validate(EnergyPlanValidate::class)->check($param);
        $info = $this->repository->get($id);
        if (!$this->repository->existsByUserId($id, $this->request->userId())) {
            return $this->error('权限不足');
        }
        if ($info['property_id'] != $param['property_id']) {
            return app('api_return')->error('非法操作');
        }
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        $this->repository->editInfo($info, $param, $this->request->userId());
        return app('api_return')->success('更新成功');
    }


    /**
     *  查询设备详情
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DbException
     *
     * @author Amos
     * 2024-05-18 17:44
     */
    public function detail(int $id)
    {
        if (!$this->repository->existsByUserId($id, $this->request->userId())) {
            return $this->error('权限不足');
        }
        $info = $this->repository->getDetail($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        return app('api_return')->success($info);
    }

    public function delete(int $id)
    {
        $param = $this->request->param([
            'property_id' => '',
        ]);
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        if ($info['property_id'] != $param['property_id']) {
            return app('api_return')->error('非法操作');
        }
        $res = $this->repository->batchDelete([$id], $this->request->userId());
        if ($res) {
            return $this->successText('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }

}